package com.ti.shangou.service.impl;

import com.ti.shangou.dao.GoodsDao;
import com.ti.shangou.dao.MerchantDao;
import com.ti.shangou.pojo.dto.PageDTO;
import com.ti.shangou.pojo.dto.ResponseDTO;
import com.ti.shangou.pojo.entity.Goods;
import com.ti.shangou.pojo.entity.GoodsType;
import com.ti.shangou.pojo.entity.Merchant;
import com.ti.shangou.pojo.query.GoodsQuery;
import com.ti.shangou.pojo.vo.GoodsTypeVO;
import com.ti.shangou.pojo.vo.GoodsVO;
import com.ti.shangou.service.GoodsService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * 作者：田宝山
 * 日期：2020/6/8
 */
@Service
public class GoodsServiceImpl implements GoodsService {
    @Resource
    GoodsDao goodsDao;
    @Resource
    MerchantDao merchantDao;

    public PageDTO ajaxGoodsList(GoodsQuery goodsQuery){
        List<GoodsVO> goodsVOS = goodsDao.selectGoodsList(goodsQuery);

        Integer count = goodsDao.selectMerchantGoodsCount(goodsQuery);

        return PageDTO.setPageData(count,goodsVOS);
    }

    public ResponseDTO addGoods(Goods goods) {
        Merchant merchant = merchantDao.selectByPrimaryKey(goods.getMerchantId());
        goods.setBusinessTypeId(merchant.getBusinessType());
        goods.setProvince(merchant.getProvince());

        if (goods.getIsCoupon() == null) {
            goods.setIsCoupon(false);
        }
        if (goods.getTakeaway() == null) {
            goods.setTakeaway(false);
        }
        if (goods.getOnSale() == null) {
            goods.setOnSale(false);
        }
        // 删除缓存图片
        deleteImgCache(goods);
        return ResponseDTO.get(goodsDao.insertSelective(goods)==1);
    }
}
